/*******************************************************************************
 * Copyright 2009 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/**
 * Place holder for copyright, needs to be replaced before release.
 */
package com.softao.ming.gwt.core.internal.runtime.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NamedNodeMap;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.softao.ming.gwt.core.runtime.client.application.IConfigurationElement;

/**
 * 
 */
public class ConfigurationElement implements IConfigurationElement {
	private final Object mParent;
	private final String mName;
	private final String mValue;
	private final Map<String, String> mAttributes;
	private final List<IConfigurationElement> mChildren;
	
	/**
	 * @param parent 
	 * @param xmlElement 
	 */
	public ConfigurationElement(Object parent, Element xmlElement) {
		mParent = parent;
		mName = xmlElement.getNodeName();
		mValue = xmlElement.getNodeValue();
		
		NamedNodeMap map = xmlElement.getAttributes();
		if (map != null && map.getLength() > 0) {
			mAttributes = new HashMap<String, String>();
			for (int i = 0; i < map.getLength(); i++) {
				Node item = map.item(i);
				mAttributes.put(item.getNodeName(), item.getNodeValue());
			}
		} else {
			mAttributes = null;
		}
		
		NodeList childNodes = xmlElement.getChildNodes();
		if (childNodes != null && childNodes.getLength() > 0) {
			mChildren = new ArrayList<IConfigurationElement>();
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node childNode = childNodes.item(i);
				if (childNode instanceof Element){
					mChildren.add(new ConfigurationElement(this, (Element)childNode));
				}
			}
		} else {
			mChildren = null;
		}
	}
	
	@Override
	public String getAttribute(String name) {
		return mAttributes.get(name);
	}

	@Override
	public String[] getAttributeNames() {
		if (mAttributes != null) {
			return mAttributes.keySet().toArray(new String[mAttributes.size()]);
		} else {
			return new String[0];
		}
	}

	@Override
	public IConfigurationElement[] getChildren() {
		if (mChildren != null) {
			return mChildren.toArray(new IConfigurationElement[mChildren.size()]);
		} else {
			return new IConfigurationElement[0];
		}
	}

	@Override
	public IConfigurationElement[] getChildren(String name) {
		List<IConfigurationElement> list = new ArrayList<IConfigurationElement>();
		if (mChildren != null) {
			for (IConfigurationElement element : mChildren) {
				if (element.getName().equals(name)) {
					list.add(element);
				}
			}
			return list.toArray(new IConfigurationElement[list.size()]);
		} else {
			return new IConfigurationElement[0];
		}
	}

	@Override
	public String getName() {
		return mName;
	}

	@Override
	public Object getParent() {
		return mParent;
	}

	@Override
	public String getValue() {
		return mValue;
	}

}
